/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cmcc.zysoft.infocollection.mobile.dao.MNewsDao;
import com.cmcc.zysoft.infocollection.model.News;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: NewsService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-12 上午11:36:56
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class MNewsService extends BaseServiceImpl<News, String> {

	@Resource
	private MNewsDao mNewsDao;
	
	/**
	 * 属性名称: mSystemUserService.
	 * 类       型: MSystemUserService.
	 */
	@Resource
	private MSystemUserService mSystemUserService;
	
	@Override
	public HibernateBaseDao<News, String> getHibernateBaseDao() {
		return this.mNewsDao;
	}
	
	/**
	 * 获取最新通知-通过该接口,显示用户未读的最新一条信息.
	 * @param usercode 手机号
	 * @return map
	 */
	public Map<String, Object> getUnreadNews(String usercode) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
		if(!list.isEmpty()) {
			if(!list.isEmpty()) {
				map.put("status", "1");
				map.put("result", this.mNewsDao.getUnreadNews(list.get(0).getUserId()));
			} else {
				map.put("status", "2");
				map.put("message", "暂无未读新闻");
			}
		} else {
			map.put("status", "2");
			map.put("message", "用户不存在或已删除");
		}
		return map;
	}
	
	/**
	 * 消息中心-通过该接口,用户可查看自己的未通过信息和省公司下发的通知公告.
	 * @param usercode 手机号
	 * @param page 页码
	 * @param pagesize 每页显示个数
	 * @return map
	 */
	public Map<String, Object> getNews(String usercode, int page, int pagesize) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
		if(!list.isEmpty()) {
			map = this.mNewsDao.getNews(usercode, list.get(0).getUserId(), page, pagesize);
		} else {
			map.put("status", "2");
			map.put("message", "用户不存在或已删除");
		}
		return map;
	}
	
	/**
	 * 通过通知ID获取通知详细信息.
	 * @param newsId 通知ID
	 * @return List<Map<String, Object>>
	 */
	public Map<String, Object> getNewsById(String newsId) {
		List<Map<String, Object>> list = this.mNewsDao.getNewsById(newsId);
		if(!list.isEmpty()) {
			return list.get(0);
		} else {
			return null;
		}
	}
}